package com.datuo.dtf.common.http;

/**
 * HTTP结果封装
 *
 * @author yibj
 */
public class HttpResult {

    private int code = HttpStatus.SC_OK;
    private String msg;
    private Object data;


    /**
     * 操作失败
     *
     * @param msg
     * @return
     */
    public static HttpResult error(String msg) {
        return result(HttpStatus.SC_INTERNAL_SERVER_ERROR, msg);
    }

    /**
     * 返回结果
     *
     * @param msg
     * @return
     */
    public static HttpResult result(int code, String msg) {
        HttpResult r = new HttpResult();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static HttpResult ok() {
        return result(HttpStatus.SC_OK, "操作成功!");
    }

    /**
     * 操作成功
     *
     * @return
     */
    public static HttpResult ok(Object data) {
        return result(HttpStatus.SC_OK, "操作成功!").data(data);
    }

    /**
     * 参数验证失败使用
     *
     * @param message 错误信息
     */
    public static HttpResult validateFailed(String message) {
        return result(HttpStatus.SC_NOT_FOUND, message);
    }

    /**
     * 未登录时使用
     */
    public static HttpResult unauthorized() {
        return result(HttpStatus.SC_UNAUTHORIZED, "暂未登录或token已经过期");
    }

    /**
     * 未授权时使用
     *
     * @param message 错误信息
     */
    public static HttpResult forbidden(String message) {
        return result(HttpStatus.SC_FORBIDDEN, "没有相关权限");
    }

    public HttpResult code(int code) {
        this.code = code;
        return this;
    }

    public HttpResult msg(String msg) {
        this.msg = msg;
        return this;
    }

    public HttpResult data(Object data) {
        this.data = data;
        return this;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "HttpResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
